import dao.*;
import entity.*;
import utils.OpenConn;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Scanner;

public class ShopCar {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner scanner = new Scanner(System.in);
        Connection connection = OpenConn.openConn();
        String[] input = null;
        while (true){
            System.out.println("请输入数字进入相应系统");
            System.out.println("输入1 用户管理，输入2 收货管理，输入3 商品管理，输入4 下单管理，输入5 分类管理，输入0 退出");
            int command = scanner.nextInt();
            if (command >5 || command < 0){
                System.out.println("输入数值非法，请重新输入");
            }
            switch (command){
                case 0:
                System.exit(0);
                case 1:

                    while (command !=0){
                        UserDao userDao = new UserDao();
                        String userInput = null;
                        User user = null;
                        int userId = 0;
                        System.out.println("请输入数字进行相应操作");
                        System.out.println("输入1 增加、输入2 删除、输入3 修改、输入4 查询，输入0 退出");
                        command = scanner.nextInt();
                        if (command >4 || command < 0){
                            System.out.println("输入数值非法，请重新输入");
                        }

                        switch (command){
                            case 1:
                                System.out.println("请输入用户名、用户性别、地址ID,用,分隔");
                                scanner.nextLine();
                                userInput = scanner.nextLine();
                                input = userInput.split("\\,");
                                user = new User(input[0],input[1],Integer.parseInt(input[2]));
                                userDao.add(user,connection);
                                System.out.println("操作完成");
                                break;
                            case 2:
                                System.out.println("请输入要删除的用户ID");
                                userId = scanner.nextInt();
                                userDao.delete(userId,connection);
                                System.out.println("操作完成");
                                break;
                            case 3:
                                System.out.println("请输入用户ID、用户名、用户性别、地址ID,用,分隔");
                                scanner.nextLine();
                                userInput = scanner.nextLine();
                                input = userInput.split("\\,");
                                user = new User(Integer.parseInt(input[0]),input[1],input[2],Integer.parseInt(input[3]));
                                userDao.update(user,connection);
                                System.out.println("操作完成");
                                break;
                            case 4:
                                System.out.println("请输入要查询的用户ID");
                                userId = scanner.nextInt();
                                user = userDao.selectById(userId,connection);
                                System.out.println(user.toString());
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                case 2:
                    while (command !=0){
                        AddressDao addressDao = new AddressDao();
                        String addressInput = null;
                        Address address = null;
                        int addressId = 0;
                        System.out.println("请输入数字进行相应操作");
                        System.out.println("输入1 增加、输入2 删除、输入3 修改、输入4 查询，输入0 退出");
                        command = scanner.nextInt();
                        if (command >4 || command < 0){
                            System.out.println("输入数值非法，请重新输入");
                        }
                        switch (command){

                            case 1:
                                System.out.println("请输入省、市、区,街，详细地址，用,分隔");
                                scanner.nextLine();
                                addressInput = scanner.nextLine();
                                input = addressInput.split("\\,");
                                address = new Address(input[0],input[1],input[2],input[3],input[4]);
                                addressDao.add(address,connection);
                                System.out.println("操作完成");
                                break;
                            case 2:
                                System.out.println("请输入要删除的地址ID");
                                addressId = scanner.nextInt();
                                addressDao.delete(addressId,connection);
                                System.out.println("操作完成");
                                break;
                            case 3:
                                System.out.println("请输入地址ID、省、市、区,街，详细地址，用,分隔");
                                scanner.nextLine();
                                addressInput = scanner.nextLine();
                                input = addressInput.split("\\,");
                                address = new Address(Integer.parseInt(input[0]),input[1],input[2],input[3],input[4],input[5]);
                                addressDao.update(address,connection);
                                System.out.println("操作完成");
                                break;
                            case 4:
                                System.out.println("请输入要查询的用户ID");
                                addressId = scanner.nextInt();
                                address = addressDao.selectById(addressId,connection);
                                System.out.println(address.toString());
                                break;
                            default:
                                break;
                        }
                    }
                break;
                case 3:
                    while (command !=0){
                        CommunityDao communityDao = new CommunityDao();
                        String communityInput = null;
                        Community community = null;
                        int communityId = 0;
                        System.out.println("请输入数字进行相应操作");
                        System.out.println("输入1 增加、输入2 删除、输入3 修改、输入4 查询，输入0 退出");
                        command = scanner.nextInt();
                        if (command >4 || command < 0){
                            System.out.println("输入数值非法，请重新输入");
                        }
                        switch (command){

                            case 1:
                                System.out.println("请输入商品名称、商品价格、商品库存,分类ID,用,分隔");
                                scanner.nextLine();
                                communityInput = scanner.nextLine();
                                input = communityInput.split("\\,");
                                community = new Community(input[0],Double.parseDouble(input[1]),Double.parseDouble(input[2]),Integer.parseInt(input[3]));
                                communityDao.add(community,connection);
                                System.out.println("操作完成");
                                break;
                            case 2:
                                System.out.println("请输入要删除的商品ID");
                                communityId = scanner.nextInt();
                                communityDao.delete(communityId,connection);
                                System.out.println("操作完成");
                                break;
                            case 3:
                                System.out.println("请输入商品ID、商品名称、商品价格、商品库存,分类ID,用,分隔");
                                scanner.nextLine();
                                communityInput = scanner.nextLine();
                                input = communityInput.split("\\,");
                                community = new Community(Integer.parseInt(input[0]),input[1],Double.parseDouble(input[2]),Double.parseDouble(input[3]),Integer.parseInt(input[4]));
                                communityDao.update(community,connection);
                                System.out.println("操作完成");
                                break;
                            case 4:
                                System.out.println("请输入要查询的商品ID");
                                communityId = scanner.nextInt();
                                community = communityDao.selectById(communityId,connection);
                                System.out.println(community.toString());
                                break;
                        }
                    }
                break;
                case 4:
                    while (command !=0){
                        OrderDao orderDao = new OrderDao();
                        String orderInput = null;
                        Order order = null;
                        int orderId = 0;
                        System.out.println("请输入数字进行相应操作");
                        System.out.println("输入1 增加、输入2 删除、输入3 修改、输入4 查询，输入0 退出");
                        command = scanner.nextInt();
                        if (command >4 || command < 0){
                            System.out.println("输入数值非法，请重新输入");
                        }
                        switch (command){

                            case 1:
                                System.out.println("请输入商品ID、购买数量,用,分隔");
                                scanner.nextLine();
                                orderInput = scanner.nextLine();
                                input = orderInput.split("\\,");
                                order = new Order(Integer.parseInt(input[0]),Integer.parseInt(input[1]));
                                orderDao.add(order,connection);
                                System.out.println("操作完成");
                                break;
                            case 2:
                                System.out.println("请输入要删除的商品ID");
                                orderId = scanner.nextInt();
                                orderDao.delete(orderId,connection);
                                System.out.println("操作完成");
                                break;
                            case 3:
                                System.out.println("请输入订单ID、商品ID、购买数量,用,分隔");
                                scanner.nextLine();
                                orderInput = scanner.nextLine();
                                input = orderInput.split("\\,");
                                order = new Order(Integer.parseInt(input[0]),Integer.parseInt(input[1]),Integer.parseInt(input[2]));
                                orderDao.update(order,connection);
                                System.out.println("操作完成");
                                break;
                            case 4:
                                System.out.println("请输入要查询的订单ID");
                                orderId = scanner.nextInt();
                                order = orderDao.selectById(orderId,connection);
                                System.out.println(order.toString());
                                break;
                        }
                    }
                break;
                case 5:
                    while (command !=0){
                        SortDao sortDao = new SortDao();
                        String sortInput = null;
                        Sort sort = null;
                        int sortId = 0;
                        System.out.println("请输入数字进行相应操作");
                        System.out.println("输入1 增加、输入2 删除、输入3 修改、输入4 查询，输入0 退出");
                        command = scanner.nextInt();
                        if (command >4 || command < 0){
                            System.out.println("输入数值非法，请重新输入");
                        }
                        switch (command){

                            case 1:
                                System.out.println("请输入分类名称");
                                scanner.nextLine();
                                sortInput = scanner.nextLine();
                                sort = new Sort(sortInput);
                                sortDao.add(sort,connection);
                                System.out.println("操作完成");

                                break;
                            case 2:
                                System.out.println("请输入要删除的商品ID");
                                sortId = scanner.nextInt();
                                sortDao.delete(sortId,connection);
                                System.out.println("操作完成");
                                break;
                            case 3:
                                System.out.println("请输入分类ID、分类名称,用,分隔");
                                scanner.nextLine();
                                sortInput = scanner.nextLine();
                                input = sortInput.split("\\,");
                                sort = new Sort(Integer.parseInt(input[0]),input[1]);
                                sortDao.update(sort,connection);
                                System.out.println("操作完成");
                                break;
                            case 4:
                                System.out.println("请输入要查询的分类ID");
                                sortId = scanner.nextInt();
                                sort = sortDao.selectById(sortId,connection);
                                System.out.println(sort.toString());
                                break;
                        }
                    }
                break;

                default:
                    break;
            }
        }
    }
}
